
interface Events {
  [index:string]: Function[]
}

interface EventBuss {
  evnets:Events;
  on:(key:string,fn:Function)=>void;
  emit:(key:string)=>void;
}

var eventBus:EventBuss = {
  evnets: {},
  on(key, fn) {
    // 把key和一个数组对应起来
    if (!this.evnets[key]) this.evnets[key] = [];
    // 把fn存进数组里面
    this.evnets[key].push(fn);
  },
  emit(key) {
    this.evnets[key]?.forEach((fn) => fn());
  },
};

export default eventBus;